﻿//****************************************************************************
//Copyright (C) 2007 flashextension.net. All Rights Reserved.
//The following is Sample Code and is subject to all restrictions on
//such code as contained in the End User License Agreement accompanying
//this product.
//****************************************************************************

import mx.transitions.Tween;
import mx.transitions.OnEnterFrameBeacon;
import flash.geom.ColorTransform;

class kliment.transitions.TweenRGB extends Tween {

	public var finishRGB:Number;
	
	public var __begin:Number;
	public var beginRed:Number;
	public var beginGreen:Number;
	public var beginBlue:Number;
	public var changeRed:Number;
	public var changeGreen:Number;
	public var changeBlue:Number;
	
	public var changeComponent:String = "Red";
	
	public  function set position (p:Number):Void {
		this.setPosition (p);		
	}
	
	public function setPosition (rgb:Number):Void {
		this.prevPos = this._pos;
		this._pos = rgb;
		
		// discern between mc and object
		if (typeof this.obj == "movieclip")	{
			var ctrans:ColorTransform = this.obj.transform.colorTransform;
			ctrans.rgb = rgb;
			this.obj.transform.colorTransform = ctrans;
		}else{
			this.obj[this.prop] = rgb;
		}
		this.broadcastMessage ("onMotionChanged", this, this._pos);	
		// added updateAfterEvent for setInterval-driven motion
		updateAfterEvent();
	}
	
	public  function get position ():Number {
		return this.getPosition();
	}
	
	function getPosition (t:Number):Number {
		if (t == undefined) t = this._time;
		var cc = this["change"+this.changeComponent];
		var new_pos = this.func (t, this["begin"+this.changeComponent], cc, this._duration);
		
		// now update the others
		var tk = (cc != 0) ? ((new_pos - this["begin"+this.changeComponent]) / cc) : 0;
		var r = (this.changeComponent == "Red") ? new_pos : this.beginRed + this.changeRed * tk;
		var g = (this.changeComponent == "Green") ? new_pos : this.beginGreen + this.changeGreen * tk;
		var b = (this.changeComponent == "Blue") ? new_pos : this.beginBlue + this.changeBlue * tk;

		return ((r << 16) | (g << 8) | b);
	};
	
	public function set begin (b:Number):Void {
		this.__begin = b;
		this.beginRed = b >> 16;
		this.beginGreen = (b >> 8) & 0xff;
		this.beginBlue = (b & 0xff);
		this.changeComponent = "Red";		
	}
	
	public function get begin():Number {
		return this.__begin;
	}
	
	public function set finish (f:Number):Void {
		this.finishRGB = f;
		
		// compute change for r,g,b		
		this.changeRed = (f >> 16) - this.beginRed;
		this.changeGreen = ((f >> 8) & 0xff) - this.beginGreen;
		this.changeBlue = (f & 0xff) - this.beginBlue;

		// use greatest delta r,g,b to pull numbers out of 'func'
		this.changeComponent = "Red";
		if (Math.abs(this.changeGreen) > Math.abs(this.changeRed)) this.changeComponent = "Green";
		if (Math.abs(this.changeBlue) > Math.abs(this["change"+this.changeComponent])) this.changeComponent = "Blue";
	}
	
	public function get finish ():Number {
		return this.finishRGB;
	}
	
/////////////////////////////////////////////////////////////////////////

/*  constructor for TweenRGB class

	obj: reference - the object which the Tween targets
	prop: string - name of the property (in obj) that will be affected
	begin: number - the starting RGB value of prop
	finish: number - the final RGB value of prop
	duration: number - the length of time of the motion; set to infinity if negative or omitted
	useSeconds: boolean - a flag specifying whether to use seconds instead of frames
*/
	function TweenRGB (obj, prop, func, begin, finish, duration, useSeconds) {
		
		OnEnterFrameBeacon.init();
		if (!arguments.length) return;
		this.obj = obj;
		this.prop = prop;
		
		// split out begin rgb
		this.begin = begin;
		
		if (func) this.func = func;
		this.finish = finish;
		
		this.position = begin;
		this.duration = duration;
		this.useSeconds = useSeconds;
		this._listeners = [];
		this.addListener (this);
		this.start();
	}
							
	
	public function toString ():String {
		return "[TweenRGB]";
	}

	///////// PRIVATE METHODS

}

